<?php
namespace app\admin\model;

use think\Model;

class Teacher extends Model{
    /**
     * 获取所有教师信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAll(){
        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['u.full_name|u.tel|u.username' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $teacher = $this
            ->alias('tea')
            ->join('user u', 'tea.teacher_id=u.id', 'LEFT')
            ->join('teacher_rank rank', 'tea.teacher_rank_id=rank.id', 'LEFT')
            ->field('tea.id,u.full_name,tea.teacher_bonus,
                    tea.teacher_card_id,tea.teacher_id,rank.teacher_rank,
                    u.rec_bonus')
            ->where($where)
            ->order($order)
            ->select();
        if($teacher){
            foreach($teacher as $k => $v){
                $teacher[$k] = $v->toArray();
            }
        }

        return $teacher;
    }

    /**
     * 获取所有学生记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getCount(){
        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['u.full_name|u.tel|u.username' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $count = $this
            ->alias('tea')
            ->join('user u', 'tea.teacher_id=u.id', 'LEFT')
            ->join('teacher_rank rank', 'tea.teacher_rank_id=rank.id', 'LEFT')
            ->field('tea.id,u.full_name,tea.teacher_bonus,
                    tea.teacher_card_id,tea.teacher_id,rank.teacher_rank,
                    u.rec_bonus')
            ->where($where)
            ->count();
        return $count;
    }

    /**
     * 查询单条信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getOne($where=""){
        $info = $this
            ->alias('tea')
            ->join('user u', 'tea.teacher_id=u.id', 'LEFT')
            ->join('teacher_rank rank', 'tea.teacher_rank_id=rank.id', 'LEFT')
            ->field('tea.id,u.full_name,u.tel,u.sex,tea.teacher_bonus,
                    tea.teacher_card_id,tea.teacher_id,rank.teacher_rank,
                    u.rec_bonus,tea.teacher_rank_id')
            ->where($where)
            ->find();
        if($info){
            $info = $info -> toArray();
        }
        return $info;
    }



    /**
     * 通过teacher表中Id查询单条信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getTeacherById($id){
        $info = $this
            ->alias('tea')
            ->join('user u', 'tea.teacher_id=u.id', 'LEFT')
            ->join('address add','tea.teacher_id = add.member_id','LEFT')
            ->join('area area','add.area_code = area.code','LEFT')
            ->join('city city','area.citycode = city.code','LEFT')
            ->join('province pro','city.provincecode = pro.code','LEFT')
            ->join('teacher_rank rank', 'tea.teacher_rank_id=rank.id', 'LEFT')
            ->field('tea.id,u.full_name,u.username,u.tel,u.user_images,
                    tea.teacher_bonus,u.nickname,
                    tea.teacher_card_id,tea.teacher_id,rank.teacher_rank,
                    u.rec_bonus,tea.teacher_rank_id,add.address,area.name as area_name,
                    area.citycode,city.name as city_name
                    ,city.provincecode,pro.name as province_name')
            ->where('tea.id='.$id)
            ->find();
        if($info){
            $info = $info -> toArray();
        }
        return $info;
    }


    /**
     * 通过teacher表中teacher_id查询单条信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getTeacherByTeacherId($id){
        $info = $this
            ->alias('tea')
            ->join('user u', 'tea.teacher_id=u.id', 'LEFT')
            ->join('address add','tea.teacher_id = add.member_id','LEFT')
            ->join('area area','add.area_code = area.code','LEFT')
            ->join('city city','area.citycode = city.code','LEFT')
            ->join('province pro','city.provincecode = pro.code','LEFT')
            ->join('teacher_rank rank', 'tea.teacher_rank_id=rank.id', 'LEFT')
            ->field('tea.id,u.full_name,u.tel,u.sex,u.username,u.nickname,
                    tea.teacher_bonus,
                    tea.teacher_card_id,tea.teacher_id,rank.teacher_rank,
                    u.rec_bonus,tea.teacher_rank_id,add.address,u.user_images,
                    area.name as area_name,area.citycode,city.name as city_name
                    ,city.provincecode,pro.name as province_name')
            ->where('tea.teacher_id='.$id)
            ->find();
        if($info){
            $info = $info -> toArray();
        }
        return $info;
    }
}